﻿using MES.Server.Entities;
using MES.Server.IService;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MES.Server.Service
{
    public class PositionService :BaseService, IPositionService
    {
       
        public PositionService(ISqlSugarClient client) : base(client)
        {

        }
        public bool CheckName(string name, int id)
        {
            return _Client.Queryable<PostEntity>()
                .Any(p => p.PostName == name &&
                        p.PostId != id);
        }
        public int Delete(string[] ids)
        {
            return _Client.Deleteable<PostEntity>()
                .In(ids).ExecuteCommand();
        }

        public IEnumerable<PostEntity> GetAllPosition(string key)
        {
            var ms = _Client.Queryable<PostEntity>()
                .Where(m =>
                string.IsNullOrEmpty(key) || m.PostName.Contains(key)
                )
                .ToList();

            return ms;
        }

        public int Update(PostEntity post)
        {
            int count;
            if (post.PostId == 0)
            {
                // 新增
                count = _Client.Insertable(post).ExecuteCommand();
            }
            else
            {
                // 更新
                count = _Client.Updateable(post).ExecuteCommand();
            }

            return count;
        }
    }
}
